package bamin.com.bamindaijia.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiton.administrator.shane_library.shane.utils.IsMobileNOorPassword;

import bamin.com.bamindaijia.R;
import bamin.com.bamindaijia.constant.Constant;

public class ChangeNameActivity extends ZjbBaseActivity implements View.OnClickListener {

    private EditText mEdt_Name;
    private String info;
    private String mType;
    private TextView mTv_title;
    private LinearLayout mLine_sex;
    private int sex =1;
    private ImageView mMan;
    private ImageView mWoman;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_name);
        init();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViews() {

        switch (mType) {
            case Constant.TYPE.CHANGE_NAME:
                initInfo("请填写姓名");
                mTv_title.setText("姓名");
                mEdt_Name.setHint("姓名");
                break;
            case Constant.TYPE.CAR_ID:
                initInfo("请填写车牌号");
                mTv_title.setText("车牌号");
                mEdt_Name.setHint("车牌号");
                break;
            case Constant.TYPE.EMERGENCY:
                initInfo("请填写紧急联系人");
                mTv_title.setText("紧急联系人");
                mEdt_Name.setHint("紧急联系人");

                break;
            case Constant.TYPE.SEX:
                mTv_title.setText("性别");
                mEdt_Name.setVisibility(View.GONE);
                mLine_sex.setVisibility(View.VISIBLE);
                if(sex==1){
                    mMan.setImageResource(R.mipmap.btn_man_selected);
                    mWoman.setImageResource(R.mipmap.btn_woman_normal);
                }else if(sex == 0){
                    mMan.setImageResource(R.mipmap.btn_man_normal);
                    mWoman.setImageResource(R.mipmap.btn_woman_selected);
                }
                break;
        }

    }

    private void initInfo(String str) {
        if (info != null) {
            if (info.equals(str)) {
                info = "";
            }
            mEdt_Name.setText(info);
        }
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageView_back).setOnClickListener(this);
        findViewById(R.id.tv_complete).setOnClickListener(this);
        mMan.setOnClickListener(this);
        mWoman.setOnClickListener(this);
    }

    @Override
    protected void initSP() {


    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        mType = intent.getStringExtra(Constant.INTENT_KEY.ADD_INFO_TYPE);
        switch (mType) {
            case Constant.TYPE.CHANGE_NAME:
                info = intent.getStringExtra(Constant.INTENT_KEY.CHANGE_NAME);
                break;
            case Constant.TYPE.CAR_ID:
                info = intent.getStringExtra(Constant.INTENT_KEY.CAR_ID);
                break;
            case Constant.TYPE.EMERGENCY:
                info = intent.getStringExtra(Constant.INTENT_KEY.EMERGENCY);

                break;
            case Constant.TYPE.SEX:
                sex = intent.getIntExtra(Constant.INTENT_KEY.SEX,1);

                break;
        }
    }

    @Override
    protected void findID() {
        mEdt_Name = (EditText) findViewById(R.id.edt_name);
        mTv_title = (TextView) findViewById(R.id.tv_title);
        mLine_sex = (LinearLayout) findViewById(R.id.line_sex);
        mMan = (ImageView) findViewById(R.id.man);
        mWoman = (ImageView) findViewById(R.id.woman);
    }
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.imageView_back:
                finishTo();
                break;
            case R.id.tv_complete:
                completeType();
                break;
            case R.id.man:
                mMan.setImageResource(R.mipmap.btn_man_selected);
                mWoman.setImageResource(R.mipmap.btn_woman_normal);
                sex = 1;
                break;
            case R.id.woman:
                mMan.setImageResource(R.mipmap.btn_man_normal);
                mWoman.setImageResource(R.mipmap.btn_woman_selected);
                sex = 0;
                break;
        }
    }

    /**
     * 完成
     */
    private void completeType() {
        Intent intent = new Intent();
        String info = mEdt_Name.getText().toString().trim();

        switch (mType) {
            case Constant.TYPE.CHANGE_NAME:
                if (!TextUtils.isEmpty(info)) {
                    intent.putExtra(Constant.INTENT_KEY.CHANGE_NAME, info);
                    setResult(Constant.REQUEST_RESULT_CODE.CHANGE_NAEM, intent);
                    finishTo();
                } else {
                    mEdt_Name.setError("请输入姓名不能为空");
                }
                break;
            case Constant.TYPE.CAR_ID:
                if (IsMobileNOorPassword.isCarID(info)) {

                    intent.putExtra(Constant.INTENT_KEY.CAR_ID, info);
                    setResult(Constant.REQUEST_RESULT_CODE.CAR_ID, intent);
                    finishTo();
                } else {
                    mEdt_Name.setError("请输入正确的车牌号,字母为大写");

                }

                break;
            case Constant.TYPE.EMERGENCY:

                if (IsMobileNOorPassword.isMobileNO(info) ) {
                    intent.putExtra(Constant.INTENT_KEY.EMERGENCY, info);
                    setResult(Constant.REQUEST_RESULT_CODE.EMERGENCY, intent);
                    finishTo();
                } else {
                    mEdt_Name.setError("请输入正确的手机号码");
                }
                break;
            case Constant.TYPE.SEX:
                    intent.putExtra(Constant.INTENT_KEY.SEX, sex);
                    setResult(Constant.REQUEST_RESULT_CODE.SEX, intent);
                    finishTo();

                break;


        }
    }
}
